package ln;

import java.util.UUID;

public record ProducerRunner(BiscuitRepository biscuitRepository, String name) implements Runnable {
    @Override
    public void run() {
        while (true) {
            synchronized (biscuitRepository) {
                if (biscuitRepository.isFull()) {
                    try {
                        biscuitRepository.wait(biscuitRepository.getTimeout());
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                } else {
                    Biscuit biscuit = new Biscuit(UUID.randomUUID().toString());
                    biscuitRepository.put(biscuit);
                    System.out.printf(
                            "%s 加入了 %s , 仓库剩余存量: %d%n",
                            this.name,
                            biscuit,
                            biscuitRepository.getSize());
                    biscuitRepository.notifyAll();
                }
            }
        }
    }
}
